/**
 * Created by limeng on 16/7/18.
 */
delegateApp.controller("ProposalSearchCtrl", function ($scope, $http) {
    $scope.pageSize = 10;
    $scope.pageNo = 1;
    $scope.inputPageNo = 1;
    $scope.pageCount = 1;
    $scope.proposals = [];
    $scope.searchMap = {};
    $scope.searchMap.proposalState = -1;

    $scope.departments = [];
    $scope.proposalStates = [
        {v:-1, name:"请选择"},
        {v:3, name:"待附议提案"},
        {v:4, name:"未立案提案"},
        {v:5, name:"转为建议的提案"},
        {v:6, name:"已立案提案"},
        {v:7, name:"已实施提案"}
    ];
    $scope.proposalTypes = [
        {v:0, name:"财务"},
        {v:1, name:"教育"},
        {v:2, name:"管理"},
        {v:3, name:"其他"}
    ];
    $http.get("/department/").success(function (data) {
        if (data) {
            $scope.departments = data;
        } else {
            bootbox.alert("获取部门信息失败!");
        }
    });

    $scope.updateContent = function() {
        $scope.searchMap.pageSize = parseInt($scope.pageSize);
        $scope.searchMap.pageNo = parseInt($scope.pageNo);
        $http.post('/proposal/search', $scope.searchMap).success(
            function (data) {
                if (data.state == true) {
                    $scope.proposals = data.list;
                    $scope.pageCount = data.pageCount;
                } else {
                    showAlert('查询失败', '原因是:' + data.result);
                }
            }
        ).error(
            function () {
                showAlert('查询失败', '请联系开发者进行维护');
            }
        );
    };

    $scope.previousPage = function () {
        $scope.pageNo = ($scope.pageNo - 1) < 1 ? 1 : --$scope.pageNo;
        $scope.updateContent();
    };
    $scope.nextPage = function() {
        $scope.pageNo = ($scope.pageNo + 1) >= $scope.pageCount ? $scope.pageCount : ++$scope.pageNo;
        $scope.updateContent();
    };
    $scope.jumpPage = function () {
        if ($scope.inputPageNo > 0 && $scope.inputPageNo <= $scope.pageCount) {
            $scope.pageNo = parseInt($scope.inputPageNo);
            $scope.updateContent();
        } else {
            $scope.inputPageNo = 1;
        }
    };

    $scope.updateContent();
});
